/*//////////////////////////////////////////////////////////////////////////
	author - BrotherhoodOfHam
	version: 1.1
	
	k1 升级优化 2022.2.6 ***脚本版***
	只有4个频道对其他玩家有效: 公共，阵营，小队（同一小队才可见），载具（载具乘员才可见）
////////////////////////////////////////////////////////////////////////////////////////////////////////*/

BH_fnc_mkr3D = {}; //防止模组版运行

if (isServer) then {
	BH_mkr3D_remote = {
		params ["", "_array"];
		_array  params ["_mkr", "_target", "_persistent"];
		[[_mkr], {
			if (isDedicated || {!hasInterface}) exitWith {};
			waitUntil {!isNil "mkr3D_intialized"};
			_this spawn BH_fnc_mkr3D;
		}] remoteExec ["BIS_fnc_spawn", _target, _persistent];
	};
	"BH_mkr3D_PVEH" addPublicVariableEventHandler BH_mkr3D_remote;
};

if (isDedicated || {!isNil "mkr3D_Channel"} || {!hasInterface}) exitWith {};

//游戏的DIK按键代码宏
#include "\a3\editor_f\data\scripts\dikCodes.h"
BH_fnc_mkr3D_toggleKey = DIK_O; //default DIK_Y (0x15)

waitUntil {!isNull player};

mkr3D_Channel = {
	_arr = toArray _this;
	_index = _arr find 47;
	_arr set [_index, 0];
	_sel = (_arr find 47) + 1;
	toString [_arr select _sel]
};

BH_fnc_mkr3D_VON = {
	_r = switch _this do {
		case "0" : {0}; //Global
		case "1" : {player getVariable ["FAR_unit_Side", side player]}; //Side
		case "2" : {player}; //Command
		case "3" : {group player}; //Group
		case "4" : {crew vehicle player}; //Vehicle
		case "5" : {player}; //Direct
		default {player};
	};
	_r
};

BH_fnc_mkr3D = {
	waitUntil {!isNull player};
	waitUntil {!isNull (findDisplay 46)};
	
	private ["_path", "_pos", "_text", "_marker", "_colour"];
	
	_marker = _this select 0;
	if (getMarkerColor _marker == "") exitWith {};
	
	_path = getText (configFile >> "cfgMarkers" >> (markerType _marker) >> "icon");
	_pos = getMarkerPos _marker;
	
	_colour = getArray (configfile >> "cfgMarkerColors" >> getMarkerColor _marker >> "color");
	if (_colour isEqualTo []) then {_colour = [1,1,1,1]};
	{
		if(typeName _x == "STRING")then{_colour set [_foreachindex, call compile _x]};
	} forEach _colour;
	
	_text = markerText _marker;
	addMissionEventHandler ["draw3D", compile format
		[
			'
				if (getMarkerColor %4 != "") then [{
					if (BH_fnc_mkr3D_show) then {drawIcon3D [%1,%2,markerPos %4,.8,.8,-(markerDir %4),%3,0,.035]};
				},{
					removeMissionEventHandler ["draw3D", _thisEventHandler]; //Arma3 v1.63
				}];
			',
			str _path,
			_colour,
			str _text,
			str _marker
		]
	];

	/*uiNamespace setVariable [_marker,
		addMissionEventHandler ["draw3D", compile format
			[
				'
					if (getMarkerColor %4 != "") then [{
						if (BH_fnc_mkr3D_show) then {drawIcon3D [%1,%2,markerPos %4,.8,.8,0,%3,0,.035]};
					},{
						removeMissionEventHandler ["draw3D", uiNamespace getVariable %4];
						uiNamespace setVariable [%4, nil];
					}];
				',
				str _path,
				_colour,
				str _text,
				str _marker
			]
		]
	];*/ //(count (toArray (getMarkerColor %4)) > 0)
};

BH_mkr3D_keydown = {
	if (_this select 1 == BH_fnc_mkr3D_toggleKey) then {
		BH_fnc_mkr3D_show = !BH_fnc_mkr3D_show;
		_format = if(BH_fnc_mkr3D_show)then[{"on"},{"off"}];
		[format ["<t size = '.7'>3D markers: %1</t>", _format],.445+safeZoneX,.07+safeZoneY,1,0,0,352] spawn BIS_fnc_dynamicText;
	};
	false
};

BH_mkr3D_MouseButtonDblClick = {
	disableSerialization;
	waitUntil {!isNull (findDisplay 54)}; //RscDisplayInsertMarker
	(findDisplay 54) displayAddEventHandler ["unload", {
		disableSerialization;
		if (_this select 1 == 1) then {
			allMapMarkers spawn {
				_mkr = (allMapMarkers - _this) select 0;
				_cha = _mkr call mkr3D_Channel;
				_target = _cha call BH_fnc_mkr3D_VON;
				_persistent = switch (typeName _target) do {
					case "SCALAR": {true};
					case "SIDE": {true};
					default {false};
				};
				BH_mkr3D_PVEH = [_mkr,_target,_persistent];
				if (isServer) then [{
					[nil, BH_mkr3D_PVEH] spawn BH_mkr3D_remote;
				},{
					publicVariableServer "BH_mkr3D_PVEH";
				}];
			};
		};
	}];
};


//旋转&移动地图标记 by k1 2020.3.20
kk_fn_getmkr = {
	private ["_markers", "_dist", "_dist2"];
	kk_mousePos = [_this select 1, _this select 2];
	_pos = (findDisplay 12 displayCtrl 51) ctrlMapScreenToWorld kk_mousePos;
	_dist = 99;
	if (isNil "kk_allMarkers") then [{
		_markers = [];
		{if(["_USER_DEFINED",_x] call BIS_fnc_inString)then{_markers pushback _x}} forEach allMapMarkers;
	},{_markers = allMapMarkers}];
	{
		_dist2 = _pos distance2D markerPos _x;
		if (_dist2 < _dist) then {
			kk_mkr_sel = _x;
			_dist = _dist2;
		};
	} forEach _markers;
	0 spawn {
		waitUntil {inputAction "lockTarget" == 0 && {inputAction "help" == 0} && {inputAction "compass" == 0}};
		kk_mousePos = nil;
		kk_mkr_sel = nil;
	};
};
kk_fn_setmkr = {
	if (inputAction "lockTarget" > 0 || {inputAction "help" > 0} || {inputAction "compass" > 0}) then {
		if (isNil "kk_mousePos") then {_this call kk_fn_getmkr};
		if ((inputAction "lockTarget" > 0 || {inputAction "help" > 0}) && {!isNil "kk_mkr_sel"}) then {
			_dir = (((kk_mousePos select 1)-(_this select 2)) atan2 ((kk_mousePos select 0)-(_this select 1))) - 90;
			kk_mkr_sel setMarkerDir _dir;
		};
		if (inputAction "compass" > 0 && {!isNil "kk_mkr_sel"}) then {
			_pos = (findDisplay 12 displayCtrl 51) ctrlMapScreenToWorld [_this select 1, _this select 2];
			kk_mkr_sel setMarkerPos _pos;
		};
	};
};

kk_mkr3D_addKey = {
	disableSerialization;
	waitUntil {!isNull (findDisplay 46)};
	kk_mkr3D_KeyDown = (findDisplay 46) displayAddEventHandler ["keydown", BH_mkr3D_keydown];
	_map = (findDisplay 12) displayCtrl 51;
	kk_mkr3D_DblClick = _map ctrlAddEventHandler ["MouseButtonDblClick", {0 spawn BH_mkr3D_MouseButtonDblClick; false}];
	kk_mkr3D_MouseMoving = _map ctrlAddEventHandler ["MouseMoving", kk_fn_setmkr]; //直接使用代码不能有注释
};

BH_fnc_mkr3D_show = true;
call kk_mkr3D_addKey;
///////////////////////////////////////////////////////////////

player createDiarySubject ["BH_3Dmarkers", "3D地图标记"];
player createDiaryRecord ["BH_3Dmarkers", ["说明", "<br/>k1编辑 2022.2.6<br/><br/>将玩家在地图上的标记3D化显示给相应频道的玩家. 按O键: 隐藏/显示<br/>只有4个频道对其他玩家有效:<br/>  公共频道（支持JIP）<br/>  阵营频道（支持JIP）<br/>  小队频道（同一小队才可见）<br/>  载具频道（载具乘员才可见）<br/><br/>鼠标在地图标记处长按T键或者H(帮助键)移动鼠标可以旋转标记，长按K(指北针键)移动标记<br/>"]];
player createDiaryRecord ["BH_3Dmarkers", ["设置&除错", "<br/><execute expression = 'if (isServer || {serverCommandAvailable ""#kick""}) then [{_str = if(isNil ""kk_allMarkers"")then[{kk_allMarkers = 1; ""全部地图标记""},{kk_allMarkers = nil; ""玩家创建的标记""}]; publicVariable ""kk_allMarkers""; systemChat _str},{systemChat ""你必须是主机或者管理员！""}];'>玩家标记/全部标记</execute>  <execute expression = 'if (isServer || {serverCommandAvailable ""#kick""}) then [{{if([""_USER_DEFINED"",_x] call BIS_fnc_inString)then{deleteMarker _x}} forEach allMapMarkers; systemChat ""执行""},{systemChat ""你必须是主机或者管理员！""}];'>删除全部玩家标记</execute>  <execute expression = 'if (!isNil {kk_mkr3D_KeyDown}) then {(findDisplay 46) displayRemoveEventHandler [""KeyDown"", kk_mkr3D_KeyDown]; disableSerialization; _map = (findDisplay 12) displayCtrl 51; _map ctrlRemoveEventHandler [""MouseButtonDblClick"", kk_mkr3D_DblClick]; _map ctrlRemoveEventHandler [""MouseMoving"", kk_mkr3D_MouseMoving]}; 0 spawn kk_mkr3D_addKey; systemChat ""执行"";'>除错</execute><br/>"]];

mkr3D_intialized = 1;

uiSleep 10;
systemChat "启用3D地图标记";


